package com.rpframework.website.device.act.admin;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.rpframework.module.adminbase.domain.AdminUser;
import com.rpframework.utils.NumberUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.domain.GoodsPro;
import com.rpframework.website.device.service.GoodsProService;
/**
 * 商品表
 * @author zhangli
 * @date 2016年3月31日 下午4:08:43
 */
@Controller
@RequestMapping("/admin/goodsPro")
public class AdminGoodsProAct extends AdminAct{
	@Resource GoodsProService service;
	
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("/list")
	public String list(@RequestParam(value="pager", required=false) Pager pager, 
			@RequestParam(value="state", required=false) Integer state, 
			@RequestParam(value="categoryId", required=false) Integer categoryId, 
			
			HttpSession session,
			Map<Object, Object> model){
		if(pager == null) {
			pager = new Pager();
		}
		pager.getSearchMap().put("builderId", "0");
		AdminUser t = (AdminUser) session.getAttribute(SESSION_ADMIN_USER_KEY);
		model.put("adminRole", t.getAdminRole().getIsSuper());
		if(NumberUtils.isValid(state)){
			pager.getSearchMap().put("state", state.toString());
			model.put("state", state);
		}
		if(NumberUtils.isValid(categoryId)){
			pager.getSearchMap().put("categoryId", categoryId.toString());
			
		}
		
		pager = service.getPager(pager);
		model.put("pager", pager);
		return this.doPackageURI("goodsPro/list");
	}
	
	
	@RequestMapping("/on")
	public @ResponseBody JsonElement onState(@RequestParam(value="id", required=false) Integer id){
		JsonObject json = new JsonObject();
	boolean flag = service.doUpdateState(id);
		if(flag){
			json.addProperty("succ", true);
		}else{
			json.addProperty("succ", false);
		}
		return json;
	}
	@RequestMapping("/off")
	public @ResponseBody JsonElement offState(@RequestParam(value="id", required=false) Integer id){
		JsonObject json = new JsonObject();
		boolean flag = service.doUpdateState(id);
		if(flag){
			json.addProperty("succ", true);
		}else{
			json.addProperty("succ", false);
		}
		return json;
	}
	

	@RequestMapping("/edit-{id}")
	public String edit(@PathVariable Integer id, Map<Object, Object> model,RedirectAttributes attr){
		GoodsPro t = service.select(id);
		Assert.notNull(t,this.getClass().getName() + " Id 异常");
		model.put("pager", t);
		return this.add(attr, model);
	}
	
	
	
	@RequestMapping("/add")
	public String add(RedirectAttributes attr,Map<Object, Object> model){
		return this.doPackageURI("goodsPro/edit");
	}
			
	
	
			
	@RequestMapping("/delete-{id}")
	public String delete(@PathVariable Integer id ,RedirectAttributes attr){
		boolean flag = service.delete(id);
		if(flag){
			setInfoMsg("删除成功！", attr);
		}else{
			setInfoMsg("删除失败！", attr);
		}
		return redirect("/admin/goodsPro/list");
	}
			
	@RequestMapping("/dosave")
	public String doSaveOrUpdate(@ModelAttribute GoodsPro t,
			RedirectAttributes attr){
		t.setBuilderId(0);
		boolean flag = service.doSaveOrUpdate(t);
		if(flag){
			if(NumberUtils.isValid(t.getId())) {
				setInfoMsg("更新操作成功！", attr);
			} else {
				setInfoMsg("新增操作成功！", attr);
			}
		}else{
			setInfoMsg("操作失败！", attr);
		}
		return redirect("/admin/goodsPro/list");
	}
	
	@RequestMapping("/{id}/detail")
	public String detail(@PathVariable Integer id,RedirectAttributes attr, Map<Object, Object> model,HttpSession session){
		GoodsPro t = service.select(id);
		Assert.notNull(t,"Not find Id: " + id);
		model.put("entity", t);
		return this.doPackageURI("goodsPro/detail");
	}
	
//	@RequestMapping("/detail")
//	public @ResponseBody JsonElement goodsDetail(
//			@RequestParam(value="name", required=false) String name
//			){
//		JsonObject json = new JsonObject();
//		Goods t = service.getGoodsByName(name);
//		if(t!=null){
//			json.addProperty("succ", true);
//		}else{
//			json.addProperty("succ", false);
//		}
//		return json;
//	}
}
